package leafedbox.managers
{
	import leafedbox.commands.Command;
	import leafedbox.events.CommandEvent;

	import flash.events.EventDispatcher;
	

	//--------------------------------------
	// Events
	//--------------------------------------

	/** 
	 * This class is the implementation of the <code>CommandManager</code>.
	 *
	 * @see CommandManager
	 */
	public class CommandManagerImpl extends EventDispatcher
	{
		//--------------------------------------------------------------------------
		// Constructor
		//--------------------------------------------------------------------------

		/**
		 * Constructor.
		 */
		public function CommandManagerImpl()
		{
			super();
		}


		//--------------------------------------------------------------------------
		// Properties
		//--------------------------------------------------------------------------

		/**
		 * @private
		 * 
		 * A lookup of remote host addresses.
		 */
		protected var remoteAddresses : Object = new Object();
		
		/**
		 * @private
		 * 
		 * A lookup of parameters by remote address and method.
		 */
		protected var parameters : Object = new Object();
		
		/**
		 * @private
		 * 
		 * A lookup of command classes.
		 */
		protected var classes : Object = new Object();
		


		//--------------------------------------------------------------------------
		// Methods
		//--------------------------------------------------------------------------

		//----------------------------------
		// Remote address
		//----------------------------------

		/**
		 * @copy CommandManager#getRemoteAddress()
		 */
		public function getRemoteAddress(name:String="default"):String
		{
			return (remoteAddresses[name] ? remoteAddresses[name] : "");
		}
		
		/**
		 * @copy CommandManager#setRemoteAddress()
		 */
		public function setRemoteAddress(name:String, url:String):void
		{
			remoteAddresses[name] = url;
		}
		
		/**
		 * @copy CommandManager#setDefaultRemoteAddress()
		 */
		public function setDefaultRemoteAddress(url:String):void
		{
			setRemoteAddress("default", url);
		}
		

		//----------------------------------
		// Global parameters
		//----------------------------------

		/**
		 * @copy CommandManager#getGlobalParameter()
		 */
		public function getGlobalParameter(name:String, method:String="GET"):*
		{
			if(parameters[method] &&
			   parameters[method][name] != null)
			{
				return parameters[method][name];
			}
			else {
				return null;
			}
		}

		/**
		 * @copy CommandManager#setGlobalParameter()
		 */
		public function setGlobalParameter(name:String, value:*, method:String="GET"):void
		{
			if(parameters[method] == null) {
				parameters[method] = new Object();
			}
			
			parameters[method][name] = value;
		}

		/**
		 * @copy CommandManager#getGlobalParameters()
		 */
		public function getGlobalParameters():Object
		{
			return parameters;
		}
		

		//----------------------------------
		// Command registration
		//----------------------------------
		
		/**
		 * @copy CommandManager#registerClass()
		 */
		public function registerClass(name:String, classRef:Class):void
		{
			classes[name] = classRef;
		}
		
		/**
		 * @copy CommandManager#unregisterClass()
		 */
		public function unregisterClass(name:String):void
		{
			delete classes[name];
		}
		
		/**
		 * @copy CommandManager#create()
		 */
		public function create(name:String):*
		{
			var classRef : Class = classes[name];
			return (classRef ? new classRef() : null);
		}

	}

}